package l1j.opqlo.C_ItemUse_PART2;

import java.sql.Timestamp;

import l1j.opqlo.NewSystem.各項記錄輸出;
import l1j.server.Config;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.LogEnchantTable;
import l1j.server.server.datatables.lock.CharItemsReading;
import l1j.server.server.model.L1PcInventory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.item.L1ItemId;
import l1j.server.server.serverpackets.S_ItemStatus;
import l1j.server.server.serverpackets.S_OwnCharStatus;
import l1j.server.server.serverpackets.S_SPMR;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Armor;
import l1j.server.server.templates.L1Item;
import l1j.server.server.utils.Random;

public class C_ScrollOfEnchant {

	public static C_ScrollOfEnchant getInstnace() {
		return new C_ScrollOfEnchant();
	}

	/** 飾品強化卷軸 BY opqlo */
	public void Accessories(final L1ItemInstance l1iteminstance,
			final L1ItemInstance l1iteminstance1, final ClientThread client)
					throws Exception {
		final L1PcInstance pc = client.getActiveChar();
		final L1Item item = l1iteminstance1.getItem();
		if (item.getType2() != 2) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		} else if (l1iteminstance1 != null
				&& (item.getType() == 8 || item.getType() == 9
				|| item.getType() == 10 || item.getType() == 12)) {
			final int chance = Random.nextInt(100) + 1;
			switch (l1iteminstance1.getEnchantLevel()) {
			case -1:
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 6:
			case 7:
			case 8:
			case 9:
				if (l1iteminstance1.getEnchantLevel() == -1) {
					l1iteminstance1.setEnchantLevel(0);
				}
				if (chance < 15) {
					l1iteminstance1.setaddHp(l1iteminstance1.getaddHp() + 2);
					if (l1iteminstance1.isEquipped()) {
						pc.addMaxHp(2);
					}
				} else if (chance > 15 && chance < 25) {
					l1iteminstance1.setaddMp(l1iteminstance1.getaddMp() + 1);
					if (l1iteminstance1.isEquipped()) {
						pc.addMaxMp(1);
					}
				} else if (chance > 25 && chance < 35) {
					l1iteminstance1.setFireMr(l1iteminstance1.getFireMr() + 1);
					l1iteminstance1
					.setWaterMr(l1iteminstance1.getWaterMr() + 1);
					l1iteminstance1
					.setEarthMr(l1iteminstance1.getEarthMr() + 1);
					l1iteminstance1.setWindMr(l1iteminstance1.getWindMr() + 1);
					if (l1iteminstance1.isEquipped()) {
						pc.addFire(1);
						pc.addWater(1);
						pc.addEarth(1);
						pc.addWind(1);
					}
				} else {
					this.FailureEnchant(pc, l1iteminstance1,
							l1iteminstance.getName());
					pc.getInventory().removeItem(l1iteminstance, 1);
					return;
				}
				break;
			case 5:
				if (chance < 15) {
					l1iteminstance1.setaddHp(l1iteminstance1.getaddHp() + 2);
					l1iteminstance1.setMpr(item.get_addmpr() + 1);
					if (l1iteminstance1.isEquipped()) {
						pc.addMaxHp(2);
					}
				} else if (chance > 15 && chance < 25) {
					l1iteminstance1.setaddMp(l1iteminstance1.getaddMp() + 1);
					l1iteminstance1.setaddSp(item.get_addsp() + 1);
					if (l1iteminstance1.isEquipped()) {
						pc.addMaxMp(1);
					}
				} else if (chance > 25 && chance < 35) {
					l1iteminstance1.setFireMr(l1iteminstance1.getFireMr() + 1);
					l1iteminstance1
					.setWaterMr(l1iteminstance1.getWaterMr() + 1);
					l1iteminstance1
					.setEarthMr(l1iteminstance1.getEarthMr() + 1);
					l1iteminstance1.setWindMr(l1iteminstance1.getWindMr() + 1);
					l1iteminstance1.setMpr(l1iteminstance1.getMpr() + 1);
					l1iteminstance1.setHpr(l1iteminstance1.getHpr() + 1);
					if (l1iteminstance1.isEquipped()) {
						pc.addFire(1);
						pc.addWater(1);
						pc.addEarth(1);
						pc.addWind(1);
					}
				} else {
					this.FailureEnchant(pc, l1iteminstance1,
							l1iteminstance.getName());
					pc.getInventory().removeItem(l1iteminstance, 1);
					return;
				}
				break;
			default:
				pc.sendPackets(new S_ServerMessage(79));
				return;
			}
			this.SuccessEnchant(pc, l1iteminstance1, client, 1);
			pc.sendPackets(new S_ItemStatus(l1iteminstance1));// TODO 線上更新
			CharItemsReading.get().updateFireMr(l1iteminstance1);
			CharItemsReading.get().updateWaterMr(l1iteminstance1);
			CharItemsReading.get().updateEarthMr(l1iteminstance1);
			CharItemsReading.get().updateWindMr(l1iteminstance1);
			CharItemsReading.get().updateaddSp(l1iteminstance1);
			CharItemsReading.get().updateaddHp(l1iteminstance1);
			CharItemsReading.get().updateaddMp(l1iteminstance1);
			CharItemsReading.get().updateHpr(l1iteminstance1);
			CharItemsReading.get().updateMpr(l1iteminstance1);
			pc.getInventory().removeItem(l1iteminstance, 1);// TODO 刪除道具
		}
		// TODO 飾品強化卷軸
	}

	private void FailureEnchant(final L1PcInstance pc,final L1ItemInstance item, final String itemName) {
		StringBuilder aa = new StringBuilder(32).append("IP(")
				.append(pc.getNetConnection().getIp().toString())
				.append(")	玩家:【").append(pc.getName()).append("】	使用 ")
				.append(itemName);
		// TODO 裝備保護卷軸 by missu0524
		if (item.getproctect() == true) {
			item.setproctect(false);
			pc.sendPackets(new S_SystemMessage("保護效果消失。"));
			pc.sendPackets(new S_ItemStatus(item));

			final int chance = Random.nextInt(100) + 1;
			if (chance >= 61) {
				final int enchantlv = item.getEnchantLevel();
				if (item.getItem().getType2() == 2 && item.isEquipped()) {
					pc.addAc(+1);
				}
				if (!item.isIdentified()) {
					item.setIdentified(true);
				}
				aa.append("	[").append(item.getLogName()).append("]		流水號:( ")
				.append(item.getId()).append(")	強化值-1");
				item.setEnchantLevel(enchantlv - 1);
				pc.sendPackets(new S_ItemStatus(item));
				pc.getInventory().saveItem(item, L1PcInventory.COL_ENCHANTLVL);
				pc.sendPackets(new S_ServerMessage(1310));

			} else {
				aa.append("	[").append(item.getLogName()).append("]		流水號:( ")
				.append(item.getId()).append(")	強化值不變");
			}
		} else {
			String s = "";
			String sa = "";
			final int itemType = item.getItem().getType2();
			final String nameId = item.getName();
			String pm = "";
			if (itemType == 1) { // 武器
				if (!item.isIdentified() || item.getEnchantLevel() == 0) {
					s = nameId; // \f1%0が強烈に%1光ったあと、蒸發してなくなります。
					sa = "$245";
				} else {
					if (item.getEnchantLevel() > 0) {
						pm = "+";
					}
					s = new StringBuilder().append(pm + item.getEnchantLevel())
							.append(" ").append(nameId).toString(); // \f1%0が強烈に%1光ったあと、蒸發してなくなります。
					sa = "$245";
				}
			} else if (itemType == 2) { // 防具
				if (!item.isIdentified() || item.getEnchantLevel() == 0) {
					s = nameId; // \f1%0が強烈に%1光ったあと、蒸發してなくなります。
					sa = " $252";
				} else {
					if (item.getEnchantLevel() > 0) {
						pm = "+";
					}
					s = new StringBuilder().append(pm + item.getEnchantLevel())
							.append(" ").append(nameId).toString(); // \f1%0が強烈に%1光ったあと、蒸發してなくなります。
					sa = " $252";
				}
			}
			pc.sendPackets(new S_ServerMessage(164, s, sa));
			aa.append("	[").append(item.getViewName()).append("]		流水號:( ")
			.append(item.getId()).append(")	爆掉了");
			pc.getInventory().removeItem(item, item.getCount());
		}
		aa.append("時間:").append(new Timestamp(System.currentTimeMillis()));
		各項記錄輸出.LogSave(16, aa.toString());
		aa.delete(0, aa.length());
		aa = null;
	}

	/*
	 * private int EnchantChance(L1ItemInstance l1iteminstance) { byte byte0 =
	 * 0; int i = l1iteminstance.getEnchantLevel(); if
	 * (l1iteminstance.getItem().getType2() == 1) { switch (i) { case 0: // '\0'
	 * byte0 = 50; break;
	 *
	 * case 1: // '\001' byte0 = 33; break;
	 *
	 * case 2: // '\002' byte0 = 25; break;
	 *
	 * case 3: // '\003' byte0 = 25; break;
	 *
	 * case 4: // '\004' byte0 = 25; break;
	 *
	 * case 5: // '\005' byte0 = 20; break;
	 *
	 * case 6: // '\006' byte0 = 33; break;
	 *
	 * case 7: // '\007' byte0 = 33; break;
	 *
	 * case 8: // '\b' byte0 = 33; break;
	 *
	 * case 9: // '\t' byte0 = 25; break;
	 *
	 * case 10: // '\n' byte0 = 20; break; } } else if
	 * (l1iteminstance.getItem().getType2() == 2) { switch (i) { case 0: // '\0'
	 * byte0 = 50; break;
	 *
	 * case 1: // '\001' byte0 = 33; break;
	 *
	 * case 2: // '\002' byte0 = 25; break;
	 *
	 * case 3: // '\003' byte0 = 25; break;
	 *
	 * case 4: // '\004' byte0 = 25; break;
	 *
	 * case 5: // '\005' byte0 = 20; break;
	 *
	 * case 6: // '\006' byte0 = 17; break;
	 *
	 * case 7: // '\007' byte0 = 14; break;
	 *
	 * case 8: // '\b' byte0 = 12; break;
	 *
	 * case 9: // '\t' byte0 = 11; break; } } return byte0; }
	 */
	/**
	 * 魔法武器卷軸改寫
	 *
	 * @throws Exception
	 */
	public void magicweaponscroll(final L1ItemInstance l1iteminstance,
			final L1ItemInstance l1iteminstance1, final L1PcInstance pc)
					throws Exception {
		final int itemId = l1iteminstance.getItemId();

		final L1ItemInstance check_item = pc.getInventory().getItem(
				l1iteminstance1.getId());
		if (check_item == null || check_item.getSkillType() > 0
				|| check_item.getItem().getType2() != 1
				|| l1iteminstance1.getBless() >= 128
				|| l1iteminstance1.getItem().get_safeenchant() <= 0) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		if (Config.ATTR_ENCHANT_CHANC >= Random.nextInt(100) + 1) {
			pc.sendPackets(new S_SystemMessage("你的武器隱藏這強大的魔力。"));
			int newsetSkillType = 0;
			switch (itemId) {
			case 49290:
				newsetSkillType = 1;// TODO 噬血
				break;
			case 49291:
				newsetSkillType = 2;// TODO 冥魔
				break;
			case 49292:
				newsetSkillType = 3;// TODO 冰雨
				break;
			case 49293:
				newsetSkillType = 4;// TODO 光爆
				break;
			}
			l1iteminstance1.setSkillType(newsetSkillType);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_SKILLTYPE);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_SKILLTYPE);
		} else {
			pc.sendPackets(new S_SystemMessage("武器受到強大的魔力保護幸好沒有銷毀。"));
		}
		pc.sendPackets(new S_ItemStatus(l1iteminstance1));// TODO 線上更新
		CharItemsReading.get().updateSkillType(l1iteminstance1);// TODO 儲存道具
		pc.getInventory().removeItem(l1iteminstance, 1);// TODO 刪除道具
	}

	/** 屬性卷軸 BY opqlo */
	public void PropertyScroll(final L1ItemInstance l1iteminstance,
			final L1ItemInstance l1iteminstance1, final L1PcInstance pc) {
		final int itemId = l1iteminstance.getItemId();

		if (l1iteminstance1 == null
				|| l1iteminstance1.getItem().getType2() != 1) {
			pc.sendPackets(new S_ServerMessage(1410, l1iteminstance1.getLogName()));// TODO 對%附加強大的魔法力量成功。
			return;
		}
		final int safeEnchant = l1iteminstance1.getItem().get_safeenchant();
		if (safeEnchant < 0) { // 強化不可
			pc.sendPackets(new S_ServerMessage(1411, l1iteminstance1.getLogName()));// TODO 對%附加魔法失敗。
			return;
		}

		if (l1iteminstance1.getBless() >= 128) { // 封印された裝備強化不可
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		// 0:無屬性 1:地 2:火 4:水 8:風 5:光 6:闇 opqlo
		final int oldAttrEnchantKind = l1iteminstance1.getAttrEnchantKind();
		final int oldAttrEnchantLevel = l1iteminstance1.getAttrEnchantLevel();

		boolean isSameAttr = false; // スクロールと強化濟みの屬性が同一か
		if (itemId == 41429 && oldAttrEnchantKind == 8 || itemId == 41430
				&& oldAttrEnchantKind == 1 || itemId == 41431
				&& oldAttrEnchantKind == 4 || itemId == 41432
				&& oldAttrEnchantKind == 2 || itemId == 245171
				&& oldAttrEnchantKind == 5// opqlo 光屬性
				|| itemId == 245172 && oldAttrEnchantKind == 6// opqlo 闇屬性
				) { // 同じ屬性
			isSameAttr = true;
		}
		if (isSameAttr && oldAttrEnchantLevel >= 10) {
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。//TODO
			// 屬性卷軸強化訊息錯誤修正
			return;
		}

		final int rnd = Random.nextInt(100) + 1;
		if (Config.ATTR_ENCHANT_CHANCE >= rnd) {
			pc.sendPackets(new S_ServerMessage(161, l1iteminstance1.getLogName(), "$245", "$247")); // \f1%0が%2%1光ります。
			int newAttrEnchantKind = 0;
			int newAttrEnchantLevel = 0;
			if (isSameAttr) { // 同じ屬性なら+1
				newAttrEnchantLevel = oldAttrEnchantLevel + 1;
			} else { // 異なる屬性なら1
				newAttrEnchantLevel = 1;
			}
			switch (itemId) {
			case 41429:
				newAttrEnchantKind = 8;
				break;// 風の武器強化スクロール
			case 41430:
				newAttrEnchantKind = 1;
				break;// 地の武器強化スクロール
			case 41431:
				newAttrEnchantKind = 4;
				break; // 水の武器強化スクロール
			case 41432:
				newAttrEnchantKind = 2;
				break;// 火の武器強化スクロール
			case 245171:
				newAttrEnchantKind = 5;
				break;// opqlo 光屬性
			case 245172:
				newAttrEnchantKind = 6;
				break;// opqlo 闇屬性
			}
			l1iteminstance1.setAttrEnchantKind(newAttrEnchantKind);
			pc.getInventory().updateItem(l1iteminstance1,L1PcInventory.COL_ATTR_ENCHANT_KIND);
			pc.getInventory().saveItem(l1iteminstance1,L1PcInventory.COL_ATTR_ENCHANT_KIND);
			l1iteminstance1.setAttrEnchantLevel(newAttrEnchantLevel);
			pc.getInventory().updateItem(l1iteminstance1,L1PcInventory.COL_ATTR_ENCHANT_LEVEL);
			pc.getInventory().saveItem(l1iteminstance1,L1PcInventory.COL_ATTR_ENCHANT_LEVEL);
		} else {
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
		}
		pc.getInventory().removeItem(l1iteminstance, 1);

	}

	/** 裝備保護卷軸 BY opqlo */
	public void ProtectionScroll(final L1ItemInstance l1iteminstance,
			final L1ItemInstance l1iteminstance1, final L1PcInstance pc) {
		if (l1iteminstance1 != null) {
			if (l1iteminstance1.getItem().get_safeenchant() <= -1) {
				pc.sendPackets(new S_ServerMessage(1309));
				return;
			}
			if (l1iteminstance1.getEnchantLevel() < l1iteminstance1.getItem()
					.get_safeenchant()) {
				pc.sendPackets(new S_SystemMessage("無法使用，該物品未達安定值。"));
				return;
			}

			if (l1iteminstance1.getproctect() == true) {
				pc.sendPackets(new S_ServerMessage(1300));
				return;
			}
			if (l1iteminstance1.getItem().getType2() == 0) {
				pc.sendPackets(new S_ServerMessage(79));
				return;

			}
			l1iteminstance1.setproctect(true);
			pc.sendPackets(new S_ServerMessage(1308, l1iteminstance1
					.getLogName()));
			pc.getInventory().removeItem(l1iteminstance, 1);

			pc.sendPackets(new S_ItemStatus(l1iteminstance1));
		}

	}

	private int RandomELevel(final L1ItemInstance item, final int itemId) {
		if (itemId == L1ItemId.B_SCROLL_OF_ENCHANT_ARMOR
				|| itemId == L1ItemId.B_SCROLL_OF_ENCHANT_WEAPON
				|| itemId == 140129 || itemId == 140130) {
			final int j = Random.nextInt(100) + 1;
			if (item.getEnchantLevel() <= 2) {
				if (j < 32) {
					return 1;
				} else if (j >= 33 && j <= 76) {
					return 2;
				} else if (j >= 77 && j <= 100) {
					return 3;
				}
			} else if (item.getEnchantLevel() >= 3
					&& item.getEnchantLevel() <= 5) {
				if (j < 50) {
					return 2;
				} else {
					return 1;
				}
			}
			{
				return 1;
			}
		}
		return 1;
	}

	/** 對盔甲施法的卷軸 BY opqlo */
	public void ScrollOfEnchantArmmor(final L1ItemInstance l1iteminstance,
			final L1ItemInstance armor, final ClientThread client) {
		int itemId;
		final L1PcInstance pc = client.getActiveChar();
		try {
			itemId = l1iteminstance.getItem().getItemId();
		} catch (final Exception e) {
			return;
		}

		if (armor == null
				|| armor.getItem().getType2() != 2) {
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		final int safe_enchant = ((L1Armor) armor.getItem())
				.get_safeenchant();
		if (safe_enchant < 0) { // 強化不可
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		if (armor.getBless() >= 128) { // 封印された裝備強化不可
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		final int armorId = armor.getItem().getItemId();
		if (armorId == 20161 || armorId >= 21035 && armorId <= 21038) { // イリュージョン防具
			if (itemId == 40127) { // イリュージョン防具強化スクロール
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		if (itemId == 40127) { // イリュージョン防具強化スクロール
			if (armorId == 20161 || armorId >= 21035 && armorId <= 21038) { // イリュージョン防具
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}

		// TODO 象牙塔對盔甲施法的卷軸byattendant
		if (armorId == 20126 || armorId == 20173 || armorId == 20206
				|| armorId == 20232 || armorId == 21138 || armorId == 20028) { // イリュージョン防具
			if (itemId == 49311) { // イリュージョン防具強化スクロール
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		if (itemId == 49311) { // イリュージョン防具強化スクロール
			if (armorId == 20126 || armorId == 20173 || armorId == 20206
					|| armorId == 20232 || armorId == 21138 || armorId == 20028) { // イリュージョン防具
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		// TODO 象牙塔對盔甲施法的卷軸byattendant

		final int enchant_level = armor.getEnchantLevel();
		// 未有防爆保護下
		if (armor.getproctect() == false){
			final int canuselv = armor.getItem().get_safeenchant() +3;
			if(enchant_level > canuselv) {
				pc.sendPackets(new S_SystemMessage("此物品超出安定值3點，必須要先防爆才可以強化。"));
				return;
			}
		}
		
		if (itemId == L1ItemId.C_SCROLL_OF_ENCHANT_ARMOR) { // c-zel
			pc.getInventory().removeItem(l1iteminstance, 1);
			if (enchant_level < -6) {
				// -7以上はできない。
				this.FailureEnchant(pc, armor,
						l1iteminstance.getName());
			} else {
				this.SuccessEnchant(pc, armor, client, -1);
			}
		} else if (enchant_level < safe_enchant) {
			pc.getInventory().removeItem(l1iteminstance, 1);
			this.SuccessEnchant(pc, armor, client,
					this.RandomELevel(armor, itemId));
		} else {
			pc.getInventory().removeItem(l1iteminstance, 1);
			final int rnd = Random.nextInt(100) + 1;
			final int enchant_chance_armor;
			final int enchant_level_tmp;
			if (safe_enchant == 0) { // 骨、ブラックミスリル用補正
				enchant_level_tmp = enchant_level + 2;
			} else {
				enchant_level_tmp = enchant_level;
			}
			if (enchant_level >= 9) {
				enchant_chance_armor = (100 + enchant_level_tmp
						* Config.ENCHANT_CHANCE_ARMOR)
						/ (enchant_level_tmp * 2);
			} else {
				enchant_chance_armor = (100 + enchant_level_tmp
						* Config.ENCHANT_CHANCE_ARMOR)
						/ enchant_level_tmp;
			}

			if (rnd < enchant_chance_armor) {
				final int randomEnchantLevel = this.RandomELevel(
						armor, itemId);
				this.SuccessEnchant(pc, armor, client,
						randomEnchantLevel);
			} else if (enchant_level >= 9 && rnd < enchant_chance_armor * 2) {
				final String item_name_id = armor.getName();
				String pm = "";
				if (enchant_level > 0) {
					pm = "+";
				}
				// \f1%0が%2と強烈に%1光りましたが、幸い無事にすみました。
				pc.sendPackets(new S_ServerMessage(160, new StringBuilder()
				.append(pm + enchant_level).append(" ")
				.append(item_name_id).toString(), "$252", "$248"));
				if (armor.getproctect() == true) {
					armor.setproctect(false);
					pc.sendPackets(new S_SystemMessage("保護效果消失。"));
					pc.sendPackets(new S_ItemStatus(armor));
				}
			} else {
				this.FailureEnchant(pc, armor,
						l1iteminstance.getName());
			}
		}
	}

	/** 對武器施法的卷軸 BY opqlo */
	public void ScrollOfEnchantWeapon(final L1ItemInstance l1iteminstance,
			final L1ItemInstance weapon, final ClientThread client) {
		final int itemId;
		final L1PcInstance pc = client.getActiveChar();
		try {
			itemId = l1iteminstance.getItem().getItemId();
		} catch (final Exception e) {
			return;
		}
		if (weapon == null
				|| weapon.getItem().getType2() != 1) {
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		final int safe_enchant = weapon.getItem().get_safeenchant();
		if (safe_enchant < 0) { // 強化不可
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		if (weapon.getBless() >= 128) { // 封印された裝備強化不可
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}

		final int quest_weapon = weapon.getItem().getItemId();
		if (quest_weapon >= 246 && quest_weapon <= 249) { // 強化不可
			if (itemId == L1ItemId.SCROLL_OF_ENCHANT_QUEST_WEAPON) { // 試練のスクロール
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		if (itemId == L1ItemId.SCROLL_OF_ENCHANT_QUEST_WEAPON) { // 試練のスクロール
			if (quest_weapon >= 246 && quest_weapon <= 249) { // 強化不可
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		final int weaponId = weapon.getItem().getItemId();
		if (weaponId == 36 || weaponId == 183 || weaponId >= 250
				&& weaponId <= 255) { // イリュージョン武器
			if (itemId == 40128) { // イリュージョン武器強化スクロール
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		if (itemId == 40128) { // イリュージョン武器強化スクロール
			if (weaponId == 36 || weaponId == 183 || weaponId >= 250
					&& weaponId <= 255) { // イリュージョン武器
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		// TODO 象牙塔對武器施法的卷軸byattendant
		if (weaponId == 35 || weaponId == 48 || weaponId >= 73
				&& weaponId < 105 && weaponId < 120 && weaponId < 147
				&& weaponId < 156 && weaponId < 174 && weaponId < 175
				&& weaponId < 224 && weaponId < 7) { // イリュージョン武器
			if (itemId == 49312) { // イリュージョン武器強化スクロール
			} else {
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}
		// TODO 象牙塔對武器施法的卷軸byattendant

		final int enchant_level = weapon.getEnchantLevel();
		// 未有防爆保護下
		if (weapon.getproctect() == false){
			final int canuselv = weapon.getItem().get_safeenchant() +4;
			if(enchant_level > canuselv) {
				pc.sendPackets(new S_SystemMessage("此物品超出安定值4點，必須要先防爆才可以強化。"));
				return;
			}
		}



		if (itemId == L1ItemId.C_SCROLL_OF_ENCHANT_WEAPON) { // c-dai
			pc.getInventory().removeItem(l1iteminstance, 1);
			if (enchant_level < -6) {
				// -7以上はできない。
				this.FailureEnchant(pc, weapon,l1iteminstance.getName());
			} else {
				this.SuccessEnchant(pc, weapon, client, -1);
			}
		} else if (enchant_level < safe_enchant) {
			pc.getInventory().removeItem(l1iteminstance, 1);
			this.SuccessEnchant(pc, weapon, client,this.RandomELevel(weapon, itemId));
		} else {
			pc.getInventory().removeItem(l1iteminstance, 1);

			final int rnd = Random.nextInt(100) + 1;
			final int enchant_chance_wepon;
			if (enchant_level >= 9) {
				enchant_chance_wepon = (100 + 3 * Config.ENCHANT_CHANCE_WEAPON) / 6;
			} else {
				enchant_chance_wepon = (100 + 3 * Config.ENCHANT_CHANCE_WEAPON) / 3;
			}

			if (rnd < enchant_chance_wepon) {
				final int randomEnchantLevel = this.RandomELevel(weapon, itemId);
				this.SuccessEnchant(pc, weapon, client,randomEnchantLevel);

			} else if (enchant_level >= 9 && rnd < enchant_chance_wepon) {
				// \f1%0が%2と強烈に%1光りましたが、幸い無事にすみました。
				pc.sendPackets(new S_ServerMessage(160, weapon.getLogName(), "$245", "$248"));
				if (weapon.getproctect() == true) {
					weapon.setproctect(false);
					pc.sendPackets(new S_SystemMessage("保護效果消失。"));
					pc.sendPackets(new S_ItemStatus(weapon));
				}
			} else {
				this.FailureEnchant(pc, weapon,l1iteminstance.getName());
			}
		}
	}

	public void SuccessEnchant(final L1PcInstance pc,
			final L1ItemInstance item, final ClientThread client, final int i) {
		// 裝備保護卷軸 by missu0524
		if (item.getproctect()) {
			pc.sendPackets(new S_SystemMessage("保護效果消失。"));
			item.setproctect(false);
			pc.sendPackets(new S_ItemStatus(item));
		}
		String s = "";
		String sa = "";
		String sb = "";
		final String s1 = item.getName();
		String pm = "";
		if (item.getEnchantLevel() > 0) {
			pm = "+";
		}
		final int Type2 = item.getItem().getType2();
		final int oldEnchantLvl = item.getEnchantLevel();
		final int newEnchantLvl = item.getEnchantLevel() + i;
		final int safe_enchant = item.getItem().get_safeenchant();
		item.setEnchantLevel(newEnchantLvl);
		client.getActiveChar().getInventory()
		.updateItem(item, L1PcInventory.COL_ENCHANTLVL);
		if (newEnchantLvl > safe_enchant) {
			client.getActiveChar().getInventory()
			.saveItem(item, L1PcInventory.COL_ENCHANTLVL);
		}
		if (Type2 == 1) {
			if (Config.LOGGING_WEAPON_ENCHANT != 0) {
				if (safe_enchant == 0
						|| newEnchantLvl >= Config.LOGGING_WEAPON_ENCHANT) {
					final LogEnchantTable logenchant = new LogEnchantTable();
					logenchant.storeLogEnchant(pc.getId(), item.getId(),
							oldEnchantLvl, newEnchantLvl);
				}
			}
			if (!item.isIdentified() || item.getEnchantLevel() == 0) {
				switch (i) {
				case -1:
					s = s1;
					sa = "$246";
					sb = "$247";
					break;

				case 1: // '\001'
					s = s1;
					sa = "$245";
					sb = "$247";
					break;

				case 2: // '\002'
					s = s1;
					sa = "$245";
					sb = "$248";
					break;

				case 3: // '\003'
					s = s1;
					sa = "$245";
					sb = "$248";
					break;
				}
			} else {
				switch (i) {
				case -1:
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$246";
					sb = "$247";
					break;

				case 1: // '\001'
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$245";
					sb = "$247";
					break;

				case 2: // '\002'
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$245";
					sb = "$248";
					break;

				case 3: // '\003'
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$245";
					sb = "$248";
					break;
				}
			}
		} else if (Type2 == 2) {
			if (Config.LOGGING_ARMOR_ENCHANT != 0) {
				if (safe_enchant == 0
						|| newEnchantLvl >= Config.LOGGING_ARMOR_ENCHANT) {
					final LogEnchantTable logenchant = new LogEnchantTable();
					logenchant.storeLogEnchant(pc.getId(), item.getId(),
							oldEnchantLvl, newEnchantLvl);
				}
			}
			if (item.isEquipped()) {
				pc.addAc(-i);
				final int i2 = item.getItem().getItemId();
				if (i2 == 20011 || i2 == 20110 || i2 == 21108 || i2 == 120011) { // マジックヘルム、マジックチェーンメイル、キャラクター名の魔法抵抗のＴシャツ
					pc.addMr(i);
					pc.sendPackets(new S_SPMR(pc));
				}
				if (i2 == 20056 || i2 == 120056 || i2 == 220056) { // マジック クローク
					pc.addMr(i * 2);
					pc.sendPackets(new S_SPMR(pc));
				}
				pc.sendPackets(new S_OwnCharStatus(pc));
			}

			if (!item.isIdentified() || item.getEnchantLevel() == 0) {
				switch (i) {
				case -1:
					s = s1;
					sa = "$246";
					sb = "$247";
					break;

				case 1: // '\001'
					s = s1;
					sa = "$252";
					sb = "$247 ";
					break;

				case 2: // '\002'
					s = s1;
					sa = "$252";
					sb = "$248 ";
					break;

				case 3: // '\003'
					s = s1;
					sa = "$252";
					sb = "$248 ";
					break;
				}
			} else {
				switch (i) {
				case -1:
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$246";
					sb = "$247";
					break;

				case 1: // '\001'
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$252";
					sb = "$247 ";
					break;

				case 2: // '\002'
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$252";
					sb = "$248 ";
					break;

				case 3: // '\003'
					s = new StringBuilder().append(pm + item.getEnchantLevel())
					.append(" ").append(s1).toString(); // \f1%0が%2%1光ります。
					sa = "$252";
					sb = "$248 ";
					break;
				}
			}
		}
		pc.sendPackets(new S_ServerMessage(161, s, sa, sb));

	}
}
